<template>
  <div class="order-cqs-container">
    <!-- 筛选和搜索区域 -->
    <div class="filter-section">
      <el-input
        v-model="searchQuery"
        placeholder="输入工单号搜索"
        style="width: 250px; margin-right: 20px;"
        clearable
        @clear="fetchOrders"
        @input="fetchOrders"
      />
    </div>

    <!-- 订单表格 -->
    <el-table
      :data="filteredOrders"
      style="width: 100%; margin-top: 20px;"
      stripe
      :header-cell-style="{ color: '#333' }"
      @row-click="handleRowClick"
    >
      <el-table-column prop="orderNum" label="订单号" sortable />
      <el-table-column
        prop="cabinetNum"
        label="快递柜"
      >
        <template #default="scope">
          <el-tag type="purple">
            {{ scope.row.cabinetBelong }}{{ scope.row.cabinetNum }}号柜
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="state"
        label="状态"
        sortable
        :filters="stateFilters"
        :filtered-value="filteredState"
        :filter-method="filterStateHandler"
      >
        <template #default="scope">
          <el-tag :type="getStateTagType(scope.row.state)">
            {{ scope.row.state }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createdAt"
        label="使用时间"
        sortable
      >
        <template #default="scope">
          {{ formatDate(scope.row.createdAt) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="updatedAt"
        label="取件时间"
        sortable
      >
        <template #default="scope">
          {{ formatDate(scope.row.updatedAt) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="quCode"
        label="取件码"
      >
        <template #default="scope">
          <el-tag :type="getTagType('quCode')">
            {{ scope.row.quCode }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import axiosService from '@/utils/request'
import { showMsg } from '@/utils/showMsg'
import { layout } from '@/utils/layout'

export default {
  name: 'OrderCqs',
  data()
  {
    return {
      orders: [], // 存储原始订单数据
      searchQuery: '', // 工单号搜索关键词
      filteredState: [], // 状态筛选值
      stateFilters: [
        { text: '已取', value: '已取' },
        { text: '未取', value: '未取' }
      ]
    }
  },
  computed: {
    // 过滤后的订单数据
    filteredOrders()
    {
      return this.orders.filter(order =>
      {
        const matchesSearch = !this.searchQuery || order.orderNum.includes(this.searchQuery)
        const matchesState = !this.filteredState.length || this.filteredState.includes(order.state)
        return matchesSearch && matchesState
      })
    }
  },
  created()
  {
    this.fetchOrders()
  },
  methods: {
    // 获取订单数据
    async fetchOrders()
    {
      try
      {
        const response = await axiosService.get('/node/ordercqs/getAll')
        if (response.code === 0)
          this.orders = response.data
        else
          showMsg(`获取订单失败`, null, 'warning')
      }
      catch (error)
      {
        layout(this, true, '登录凭证失效,清重新登录')
      }
    },

    // 格式化日期为 YYYY-MM-DD HH:MM:SS
    formatDate(dateStr)
    {
      return new Date(dateStr).toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
        hour12: false
      }).replace(/\//g, '-').replace(/\b(\d)\b/g, '0$1')
    },

    // 获取状态的 Tag 颜色类型
    getStateTagType(state)
    {
      const stateColors = {
        已取: 'info', // 灰色
        未取: 'success' // 绿色
      }
      return stateColors[state] || 'info' // 默认使用 info (灰色)
    },

    // 获取其他 Tag 颜色类型
    getTagType(value)
    {
      const tagColors = {
        quCode: 'warning' // 黄色
      }
      return tagColors[value] || 'info' // 默认使用 info (蓝色)
    },

    // 过滤状态
    filterStateHandler(value, row)
    {
      return !value.length || value.includes(row.state)
    },

    // 点击行复制取件码
    handleRowClick(row)
    {
      navigator.clipboard.writeText(row.quCode)
        .then(() =>
        {
          showMsg(`已复制取件码: ${row.quCode}`, null, 'success')
        })
        .catch(err =>
        {
          showMsg('复制失败，请手动复制', err, 'warning')
        })
    }
  }
}
</script>

<style scoped>
.order-cqs-container {
  padding: 20px;
  user-select: none;
  min-height: calc(100vh - 190px);
  margin-left: 100px;
  color: #333; /* 深灰色文字，白色背景下更明显 */
}

.filter-section {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}

:deep(.el-table) {
  background-color: #fff;
  border-radius: 0;
  overflow: hidden;
}

:deep(.el-table__body tr:hover > td) {
  background-color: #f5f5f5 !important;
}

:deep(.el-table__row) {
  transition: all 0.3s ease;
}

:deep(.el-table__header th) {
  background-color: #f0f0f0; /* 浅灰色表头 */
}
</style>
